﻿package com.riaidea.text {
    import flash.display.*;
    import flash.utils.*;
    import com.riaidea.text.*;
    import flash.events.*;
    import flash.text.*;
    import flash.geom.*;

    class SpriteRenderer {

        private var _rtf:RichTextField;
        private var _numSprites:int;
        private var _spriteContainer:Sprite;
        private var _spriteIndices:Dictionary;

        public function SpriteRenderer(_arg1:RichTextField){
            this._rtf = _arg1;
            this._numSprites = 0;
            this._spriteContainer = new Sprite();
            this._spriteIndices = new Dictionary();
        }
        function render():void{
            if (this._numSprites > 0){
                this._spriteContainer.addEventListener(Event.ENTER_FRAME, this.onEnterFrame);
            };
        }
        private function onEnterFrame(_arg1:Event):void{
            this._spriteContainer.removeEventListener(Event.ENTER_FRAME, this.onEnterFrame);
            this.doRender();
        }
        private function doRender():void{
            this._spriteContainer.y = -(this.textRenderer.scrollHeight);
            this.renderVisibleSprites();
        }
        private function renderVisibleSprites():void{
            var _local5:DisplayObject;
            var _local1:int = (this.textRenderer.scrollV - 1);
            var _local2:int = (this.textRenderer.bottomScrollV - 1);
            var _local3:int = this.textRenderer.getLineOffset(_local1);
            var _local4:int = ((this.textRenderer.getLineOffset(_local2) + this.textRenderer.getLineLength(_local2)) - 1);
            while (this._spriteContainer.numChildren > 0) {
                this._spriteContainer.removeChildAt(0);
            };
            while (_local3 <= _local4) {
                if (this._rtf.isSpriteAt(_local3)){
                    _local5 = this.getSprite(_local3);
                    if (_local5 != null){
                        this.renderSprite(_local5, _local3);
                    };
                };
                _local3++;
            };
        }
        private function renderSprite(_arg1:DisplayObject, _arg2:int):void{
            var _local4:Number;
            var _local5:TextLineMetrics;
            var _local3:Rectangle = this.textRenderer.getCharBoundaries(_arg2);
            if (_local3 != null){
                _arg1.x = ((((_local3.x + ((_local3.width - _arg1.width) * 0.5)) + 0.5) - this.textRenderer.scrollH) >> 0);
                _local4 = ((_local3.height - _arg1.height) * 0.5);
                _local5 = this.textRenderer.getLineMetrics(this.textRenderer.getLineIndexOfChar(_arg2));
                if ((_local4 + _arg1.height) < _local5.ascent){
                    _local4 = (_local5.ascent - _arg1.height);
                };
                _arg1.y = (((_local3.y + _local4) + 0.5) >> 0);
                this._spriteContainer.addChild(_arg1);
            };
        }
        function adjustSpritesIndex(_arg1:int, _arg2:int):Boolean{
            var _local4:*;
            var _local5:int;
            var _local3:Boolean;
            for (_local4 in this._spriteIndices) {
                _local5 = int(_local4.name);
                if (_local5 > (_arg1 - _arg2)){
                    _local4.name = (_local5 + _arg2);
                    _local3 = true;
                };
            };
            return (_local3);
        }
        function insertSprite(_arg1:DisplayObject, _arg2:int):void{
            if (this._spriteIndices[_arg1] == null){
                _arg1.name = String(_arg2);
                this._spriteIndices[_arg1] = true;
                this._numSprites++;
            };
        }
        function removeSprite(_arg1:int):void{
            var _local2:DisplayObject = this.getSprite(_arg1);
            if (_local2 != null){
                if (this._spriteContainer.contains(_local2)){
                    this._spriteContainer.removeChild(_local2);
                };
                delete this._spriteIndices[_local2];
                this._numSprites--;
            };
        }
        function getSprite(_arg1:int):DisplayObject{
            var _local2:*;
            for (_local2 in this._spriteIndices) {
                if (_arg1 == int(_local2.name)){
                    return (_local2);
                };
            };
            return (null);
        }
        function getSpriteIndex(_arg1:DisplayObject):int{
            if (this._spriteIndices[_arg1] == true){
                return (int(_arg1.name));
            };
            return (-1);
        }
        function clear():void{
            var _local1:*;
            while (this._spriteContainer.numChildren > 0) {
                this._spriteContainer.removeChildAt(0);
            };
            for (_local1 in this._spriteIndices) {
                delete this._spriteIndices[_local1];
            };
            this._numSprites = 0;
        }
        private function get textRenderer():TextRenderer{
            return ((this._rtf.textfield as TextRenderer));
        }
        function get container():Sprite{
            return (this._spriteContainer);
        }
        function get numSprites():int{
            return (this._numSprites);
        }
        function exportXML():XML{
            var _local2:*;
            var _local5:Object;
            var _local6:XML;
            var _local1:Array = [];
            for (_local2 in this._spriteIndices) {
                _local5 = {
                    src:getQualifiedClassName(_local2),
                    index:_local2.name
                };
                _local1.push(_local5);
            };
            if (_local1.length > 1){
                _local1.sortOn("index", Array.NUMERIC);
            };
            var _local3:XML = <sprites/>
            ;
            var _local4:int;
            while (_local4 < _local1.length) {
                _local6 = new XML((((("<sprite src=\"" + _local1[_local4].src) + "\" index=\"") + (_local1[_local4].index - _local4)) + "\"/>"));
                _local3.appendChild(_local6);
                _local4++;
            };
            return (_local3);
        }

    }
}//package com.riaidea.text 
